/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.dao.hibernate;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import unilearn.dao.SubmissionDAO;
import unilearn.vo.Submission;

/**
 *
 * @author Yat
 */
public class SubmissionDAOImpl extends HibernateDAO implements SubmissionDAO {

    public SubmissionDAOImpl() {
        super();
    }

    public SubmissionDAOImpl(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<Submission> getAll() throws Exception {
        return super.getAll(Submission.class.getName());
    }

    @Override
    public Submission get(int id) throws Exception {
        Session s = sessionFactory.openSession();
        Transaction t = s.beginTransaction();

        try {
            Query query = s.createQuery("from " + Submission.class.getName() + " su where su.id = " + id);
            List<Submission> sus = query.list();
            if ((sus == null) || (sus.isEmpty())) {
                return null;
            }

            return sus.get(0);
        } finally {
            t.commit();
            s.close();
        }
    }

    @Override
    public List<Submission> find(Submission submission) throws Exception {
        return (List<Submission>) super.find(submission);
    }

    @Override
    public Submission insert(Submission submission) throws Exception {
        super.insert(submission);
        return submission;
    }

    @Override
    public Submission update(Submission submission) throws Exception {
        super.update(submission);
        return submission;
    }

    @Override
    public void delete(Submission submission) throws Exception {
        super.delete(submission);
    }
}
